﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.Utils.Design;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 表格下拉控件
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    [ToolboxItem(true)]
    [DefaultEvent("SelectedIndexChanged")]
    public class PopupGridControlEdit : ButtonEdit
    {
        static PopupGridControlEdit()
        {
            RepositoryItemPopupGridControlEdit.RegisterPopupGridControlEdit();
        }

        public PopupGridControlEdit()
        {
        }

        #region 私有变量

        private GridControlPlus _gridControlControl;
        private GridViewPlus gridView;
        private Form ownerForm;
        private const int popupDefaultSize = 200;
        private bool isFilter = true;
        private bool isAddGrid;
        private object _selectedValue;
        private CurrencyManager dataManager;
        #endregion

        #region 属性

        /// <summary>
        /// 表格Id
        /// </summary>
        [Category("XCI"),Description("表格ID")]
        public virtual string Id { get; set; } = $"popup-grid-{StringHelper.Guid()}";

        /// <summary>
        /// 指示要为此控件中的项显示的属性
        /// </summary>
        [Category("XCI"), Description("指示要为此控件中的项显示的属性")]
        public virtual string DisplayMember { get; set; }

        /// <summary>
        /// 指示用作控件中项的实际值的属性
        /// </summary>
        [Category("XCI"), Description("指示用作控件中项的实际值的属性")]
        public virtual string ValueMember { get; set; }

        /// <summary>
        /// 单击文本框显示弹出框
        /// </summary>
        [Category("XCI"), DefaultValue(true), Description("单击文本框显示弹出框")]
        public virtual bool IsClickShowPopup { get; set; } = true;

        /// <summary>
        /// 文本框获得焦点显示弹出框
        /// </summary>
        [Category("XCI"), DefaultValue(true), Description("文本框获得焦点显示弹出框")]
        public virtual bool IsFocusShowPopup { get; set; } = true;

        /// <summary>
        /// 文本框按下键盘按键显示弹出框
        /// </summary>
        [Category("XCI"), DefaultValue(true), Description("文本框按下键盘按键显示弹出框")]
        public virtual bool IsKeyPressShowPopup { get; set; } = true;

        /// <summary>
        /// 自动选中首行
        /// </summary>
        [Category("XCI"), DefaultValue(true), Description("自动选中首行")]
        public virtual bool IsSelectFirstRow { get; set; } = true;

        /// <summary>
        /// 是否强制选择
        /// </summary>
        [Category("XCI"), DefaultValue(true), Description("是否强制选择")]
        public virtual bool IsForceSelection { get; set; } = true;

        /// <summary>
        /// 是否显示时自动过滤
        /// </summary>
        [Category("XCI"), DefaultValue(false), Description("是否显示时自动过滤")]
        public virtual bool IsShowAutoFilter { get; set; }

        /// <summary>
        /// 是否允许设置表格属性
        /// </summary>
        [Category("XCI"), Browsable(true), DefaultValue(true), Description("是否允许设置表格属性")]
        public virtual bool AllowCustomSetting { get; set; } = true;

        [Category("XCI"), Description("自动加载配置"), DefaultValue(true)]
        [SmartTagProperty("自动加载配置", "XCI", SmartTagActionType.RefreshAfterExecute)]
        public bool AutoLoadSetting { get; set; } = true;

        /// <summary>
        /// 循环选择(下翻到最后一行时自动回到首行,上翻到首行时自动回到末行)
        /// </summary>
        [Category("XCI"), DefaultValue(false), Description("循环选择(下翻到最后一行时自动回到首行,上翻到首行时自动回到末行)")]
        public virtual bool IsLoopSelection { get; set; }

        /// <summary>
        /// 是否点击表格行确认选择,否则双击选择
        /// </summary>
        [Category("XCI"), DefaultValue(false), Description("点击表格行确认选择")]
        public virtual bool IsClickSelected { get; set; }

        /// <summary>
        /// 获取或设置弹出框大小(默认高度200,宽度与文本框一样)
        /// </summary>
        [Category("XCI"), DefaultValue(0), Description("弹出框大小(默认高度200,宽度与文本框一样)")]
        public virtual Size PopupSize { get; set; }

        /// <summary>
        /// 获取或设置弹出框显示位置(默认为文本框下面)
        /// </summary>
        [Category("XCI"), DefaultValue(0), Description("获取或设置弹出框显示位置(默认为文本框下面)")]
        public Point PopupPosition { get; set; }

        /// <summary>
        /// 弹出框是否打开
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsPopupOpen { get; private set; }

        /// <summary>
        /// 控件所在窗口
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        private Form OwnerForm => ownerForm ?? (ownerForm = FindForm());

        /// <summary>
        /// 对象类型
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual Type ModelType { get; set; }

        /// <summary>
        /// 表格
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public GridControlPlus GridControl => _gridControlControl;

        /// <summary>
        /// 表格数据源
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object DataSource
        {
            get => _gridControlControl.DataSource;
            set
            {
                if (WinFormHelper.IsDesignMode()) return;
                _gridControlControl.DataSource = value;
                if (((value != null) && (BindingContext != null)) && (value != Convert.DBNull))
                {
                    dataManager = (CurrencyManager)this.BindingContext[value];
                }
            }
        }

        /// <summary>
        /// 文本值
        /// </summary>
        [Bindable(true), Browsable(false)]
        public override string Text
        {
            get => base.Text;
            set => base.Text = value;
        }

        /// <summary>
        /// 获取或设置指定当前选定值。
        /// </summary>
        [Category("Data"), Description("获取或设置指定当前选定值。")]
        [Bindable(true)]
        [DefaultValue(null)]
        public new object EditValue
        {
            get => SelectedValue;
            set => SelectedValue = value;
        }

        /// <summary>
        /// 获取当前选定的对象
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object SelectedObject
        {
            get { return CurrencyManagerHelper.GetItem(dataManager, ValueMember, SelectedValue); }
        }

        /// <summary>
        /// 获取或者设置选中的值
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object SelectedValue
        {
            get
            {
                if (WinFormHelper.IsDesignMode())
                {
                    return null;
                }
                if (ValueMember.IsEmpty()) throw new ArgumentException("请指定ValueMember");
                if (dataManager == null) return null;
                return _selectedValue;
            }
            set
            {
                if (WinFormHelper.IsDesignMode())
                {
                    return;
                }
                if (ValueMember.IsEmpty()) throw new ArgumentException("请指定ValueMember");
                if (dataManager == null) return;

                var isSelectedValueChanged = _selectedValue != value;
                _selectedValue = value;

                SetClearButtonStatus(SelectedIndex > -1);
                UpdateText();

                if (isSelectedValueChanged)
                {
                    SelectedValueChanged?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// 获取或设置指定当前选定项的索引。(数据源中的行索引,不是控件中的顺序)
        /// </summary>
        /// <remarks>当前选定项的从零开始的索引。如果未选定任何项，则返回值为负一 (-1)。</remarks>
        [Category("Data"), Description("获取或设置指定当前选定项的索引。")]
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int SelectedIndex
        {
            get { return CurrencyManagerHelper.GetIndex(dataManager, ValueMember, SelectedValue); }
            set
            {
                var oldIndex = CurrencyManagerHelper.GetIndex(dataManager, ValueMember, SelectedValue);
                if (oldIndex != value)
                {
                    SelectedValue = value;
                }
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 选中值发生变化事件
        /// </summary>
        [Category("XCI"), Description("选中值发生变化事件")]
        public event EventHandler<EventArgs> SelectedValueChanged;

        #endregion

        #region 方法

        /// <summary>
        /// 初始化表格控件
        /// </summary>
        protected void InitializeGrid()
        {
            if (isAddGrid) return;
            this._gridControlControl = new GridControlPlus();
            this.gridView = new GridViewPlus();
            ((System.ComponentModel.ISupportInitialize)(this._gridControlControl)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView)).BeginInit();
            // 
            // gridControl
            // 
            this._gridControlControl.Id = Id;
            this._gridControlControl.ModelType = ModelType;
            this._gridControlControl.MainView = this.gridView;
            this._gridControlControl.Name = "gridControl";
            this._gridControlControl.AllowCustomSetting = AllowCustomSetting;
            this._gridControlControl.AutoLoadSetting = AutoLoadSetting;
            this._gridControlControl.TabStop = false;
            this._gridControlControl.ViewCollection.Add(this.gridView);
            this._gridControlControl.ProcessGridKey += this.GridControlControlProcessGridKey;
            this._gridControlControl.Location = new Point(-500, -500);
            this._gridControlControl.Size = new Size(100, 100);
            // 
            // gridView
            // 
            this.gridView.GridControl = this._gridControlControl;
            this.gridView.Name = "gridView";
            this.gridView.OptionsView.ShowGroupPanel = false;
            this.gridView.OptionsBehavior.Editable = false;
            this.gridView.OptionsCustomization.AllowGroup = false;
            this.gridView.OptionsCustomization.AllowFilter = false;
            this.gridView.OptionsSelection.EnableAppearanceHideSelection = false;
            this.gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
            this.gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;
            this.gridView.OptionsView.ColumnAutoWidth = false;
            this.gridView.OptionsSelection.MultiSelect = false;
            this.gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            this.gridView.OptionsView.ShowIndicator = false;
            this.gridView.OptionsBehavior.AutoPopulateColumns = true;
            this.gridView.SelectionChanged += this.gridView_SelectionChanged;
            this.gridView.RowClick += gridView_RowClick;
            this.gridView.DoubleClick += this.gridView_DoubleClick;
            this.gridView.CustomDrawRowIndicator += GridView_CustomDrawRowIndicator;
            ((System.ComponentModel.ISupportInitialize)(this._gridControlControl)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView)).EndInit();

            if (OwnerForm != null && !OwnerForm.Controls.Contains(_gridControlControl))
            {
                OwnerForm.Controls.Add(_gridControlControl);
                isAddGrid = true;
            }
        }

        private void GridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (gridView.OptionsView.ShowIndicator)
            {
                if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                    e.Info.ImageIndex = -1;
                }
            }
        }

        private void UpdateText()
        {
            this.isFilter = false;
            if (SelectedObject == null)
            {
                Text = string.Empty;
            }
            else
            {
                var value = CurrencyManagerHelper.GetValue(dataManager, SelectedObject, DisplayMember);
                Text = value == null ? string.Empty : value.ToString();
            }
            this.isFilter = true;
        }

        /// <summary>
        /// 显示弹出框
        /// </summary>
        public void ShowPopup()
        {
            if (IsPopupOpen) return;

            gridView.ClearSorting();
            gridView.OptionsCustomization.AllowSort = false;
            gridView.OptionsCustomization.AllowGroup = false;

            var dpiRatio = WinFormHelper.GetDpiRatio();
            var popupSize = new Size(Convert.ToInt32(PopupSize.Width * dpiRatio), Convert.ToInt32(PopupSize.Height * dpiRatio));
            _gridControlControl.Size = popupSize;
            _gridControlControl.Width = _gridControlControl.Width <= 0 ? this.Width : _gridControlControl.Width;
            int heigth = _gridControlControl.Height <= 0 ? popupDefaultSize : _gridControlControl.Height;

            _gridControlControl.Height = heigth;

            if (PopupPosition == Point.Empty)
            {
                Point p = PointToScreen(new Point(0, Height - 1));
                _gridControlControl.Location = OwnerForm.PointToClient(p);
            }
            else
            {
                _gridControlControl.Location = PopupPosition;
            }

            _gridControlControl.Visible = true;
            _gridControlControl.BringToFront();

            this.AddMouseDownEvent(OwnerForm);
            if (IsShowAutoFilter)
            {
                Filter();
            }
            else
            {
                SelectRow();
            }
            IsPopupOpen = true;
        }

        private void Complete()
        {
            if (ValueMember.IsEmpty()) throw new ArgumentException("请指定ValueMember");
            var rows = gridView.GetSelectedRows();
            if (rows.Length > 0)
            {
                var row = gridView.GetRow(rows[0]);
                this.SelectedValue = CurrencyManagerHelper.GetValue(dataManager, row, ValueMember);
            }
            else if (IsForceSelection)
            {
                this.SelectedValue = null;
            }

            SetClearButtonStatus(rows.Length > 0);
            this.SelectionStart = this.Text.Length;
        }

       
        /// <summary>
        /// 隐藏弹出框
        /// </summary>
        public void HidePopup()
        {
            this.Select();
            this.SelectionStart = this.Text.Length;
            this.SelectAll();
            _gridControlControl.Visible = false;
            IsPopupOpen = false;
            gridView.ApplyFindFilter(null);
            RemoveMouseDownEvent(OwnerForm);
        }

        private void ResetControl()
        {
            this.isFilter = false;
            gridView.ApplyFindFilter(null);
            gridView.ClearSelection();
            gridView.MakeRowVisible(0);
            this.SelectedValue = null;

            SetClearButtonStatus(false);

            this.isFilter = true;
        }

        private void Filter()
        {
            int length = this.Text.Length;
            this.SelectionStart = length;
            gridView.ApplyFindFilter(this.Text.Trim());
            if (length > 0 && gridView.RowCount > 0)
            {
                if (gridView.SelectedRowsCount == 0)
                {
                    gridView.SelectRow(gridView.FocusedRowHandle);
                }
                gridView.MoveFirst();
            }
            else
            {
                gridView.ClearSelection();
            }
        }

        private void SetClearButtonStatus(bool condition)
        {
            if (Properties.Buttons.Count <= 1) return;
            if (Properties.Buttons[1].Kind != ButtonPredefines.Clear) return;

            Properties.Buttons[1].Visible = condition;
        }

        private void SelectRow()
        {
            gridView.ClearSelection();
            if (SelectedIndex > -1)
            {
                int dataIndex = gridView.GetRowHandle(SelectedIndex);
                gridView.SelectRow(dataIndex);
                gridView.FocusedRowHandle = dataIndex;
                gridView.MakeRowVisible(dataIndex);
            }
            else if (IsSelectFirstRow)
            {
                gridView.SelectRow(0);
                gridView.FocusedRowHandle = 0;
                gridView.MakeRowVisible(0);
            }
        }

        #endregion

        #region 控件事件

        private void AddMouseDownEvent(Control control)
        {
            if ((control == null) || (control == this) || (control == _gridControlControl)) return;
            control.MouseDown += Control_MouseDown;
            foreach (Control c in control.Controls)
            {
                this.AddMouseDownEvent(c);
            }
        }

        private void RemoveMouseDownEvent(Control control)
        {
            if ((control == null) || (control == this) || (control == _gridControlControl)) return;
            control.MouseDown -= Control_MouseDown;
            foreach (Control c in control.Controls)
            {
                this.RemoveMouseDownEvent(c);
            }
        }

        private void Control_MouseDown(object sender, MouseEventArgs e)
        {
            if (IsForceSelection)
            {
                Complete();
            }
            HidePopup();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            InitializeGrid();
            if (Properties.Buttons.Count > 1 && Properties.Buttons[1].Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Clear)
            {
                Properties.Buttons[1].Visible = false;
                Properties.Buttons[1].Click += ComboGridEditx_Click;
            }
        }

        private void ComboGridEditx_Click(object sender, EventArgs e)
        {
            HidePopup();
            ResetControl();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (IsFocusShowPopup)
            {
                ShowPopup();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            if (e.Delta > 0)
            {
                gridView.MovePrev();
            }
            else
            {
                gridView.MoveNext();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (IsClickShowPopup)
            {
                ShowPopup();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void OnEditValueChanged()
        {
            base.OnEditValueChanged();

            if (isFilter)
            {
                if (IsKeyPressShowPopup)
                {
                    ShowPopup();
                }
                Filter();
                //SetDynamicsSize();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Tab || (keyData == (Keys.Tab | Keys.Shift)))
            {
                if (IsForceSelection)
                {
                    Complete();
                }
                HidePopup();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                HidePopup();
                Complete();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (!IsPopupOpen)
                {
                    ShowPopup();
                    e.Handled = true;
                    return;
                }
                if (gridView.SelectedRowsCount == 0)
                {
                    gridView.SelectRow(gridView.FocusedRowHandle);
                }
                if (IsLoopSelection && gridView.FocusedRowHandle == 0)
                {
                    gridView.MoveLastVisible();
                }
                else if (gridView.GetSelectedRows().Length == 0)
                {
                    gridView.MoveFirst();
                }
                else
                {
                    gridView.MovePrev();
                }
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (!IsPopupOpen)
                {
                    ShowPopup();
                    e.Handled = true;
                    return;
                }
                if (gridView.SelectedRowsCount == 0)
                {
                    gridView.SelectRow(gridView.FocusedRowHandle);
                }
                if (IsLoopSelection && (gridView.RowCount - 1 == gridView.FocusedRowHandle))
                {
                    gridView.MoveFirst();
                }
                else if (gridView.GetSelectedRows().Length == 0)
                {
                    gridView.MoveFirst();
                }
                else
                {
                    gridView.MoveNext();
                }
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Back)
            {
                if (this.Text.Length == 0 || this.Text.Length == 1)
                {
                    this.Text = string.Empty;
                    ResetControl();
                }
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Delete)
            {
                ResetControl();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Escape)
            {
                if (IsForceSelection)
                {
                    Complete();
                }
                HidePopup();
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        private void GridControlControlProcessGridKey(object sender, KeyEventArgs e)
        {
            if (gridView.IsGroupRow(gridView.FocusedRowHandle)) return;
            if (this.Properties.TextEditStyle == DevExpress.XtraEditors.Controls.TextEditStyles.Standard)
            {
                if ((e.KeyValue >= 65 && e.KeyValue <= 90)
                || (e.KeyValue >= 48 && e.KeyValue <= 57))
                {
                    this.Text += Convert.ToChar(e.KeyValue).ToString().ToLower();
                    this.SelectionStart = this.Text.Length;
                    e.Handled = true;
                }
            }
            if (e.KeyCode == Keys.Enter)
            {
                HidePopup();
                Complete();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Delete)
            {
                ResetControl();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Back)
            {
                if (this.Text.Length > 0)
                {
                    this.Text = this.Text.Substring(0, this.Text.Length - 1);
                    this.SelectionStart = this.Text.Length;
                }
                if (this.Text.Length == 0)
                {
                    ResetControl();
                }
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Escape)
            {
                if (IsForceSelection)
                {
                    Complete();
                }
                HidePopup();
                e.Handled = true;
            }
        }

        private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (gridView.SelectedRowsCount > 1)
            {
                gridView.ClearSelection();
                gridView.SelectRow(gridView.FocusedRowHandle);
            }
        }

        private void gridView_RowClick(object sender, RowClickEventArgs e)
        {
            if (e.RowHandle > -1 && gridView.GetSelectedRows().Length > 0 && e.Button == MouseButtons.Left && IsClickSelected)
            {
                HidePopup();
                Complete();
            }
        }

        private void gridView_DoubleClick(object sender, EventArgs e)
        {
            if (e is DXMouseEventArgs ev)
            {
                GridHitInfo info = gridView.CalcHitInfo(ev.Location);
                if (ev.Clicks == 2 && ev.Button == MouseButtons.Right)
                {
                    return;
                }
                if (ev.Clicks == 2 &&
                    (info.HitTest == GridHitTest.ColumnEdge
                     || info.HitTest == GridHitTest.EmptyRow
                     || info.HitTest == GridHitTest.Column
                     || info.HitTest == GridHitTest.ColumnButton))
                {
                    return;
                }
            }
            if (gridView.FocusedRowHandle > -1)
            {
                if (!IsClickSelected)
                {
                    HidePopup();
                    Complete();
                }
            }
        }

        #endregion

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemPopupGridControlEdit Properties => base.Properties as RepositoryItemPopupGridControlEdit;

        public override string EditorTypeName => RepositoryItemPopupGridControlEdit.CustomEditName;
    }

    [UserRepositoryItem("RegisterPopupGridControlEdit")]
    public class RepositoryItemPopupGridControlEdit : RepositoryItemButtonEdit
    {
        static RepositoryItemPopupGridControlEdit()
        {
            RegisterPopupGridControlEdit();
        }

        public const string CustomEditName = "PopupGridControlEdit";

        public RepositoryItemPopupGridControlEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterPopupGridControlEdit()
        {
            Bitmap img = null;
            try
            {
                var stream = Assembly.GetExecutingAssembly()
                    .GetManifestResourceStream("XCI.Windows.Images.SearchLookUpEdit.bmp");
                if (stream != null)
                {
                    img = (Bitmap)Image.FromStream(stream, true);

                    img.MakeTransparent(Color.Magenta);
                }
            }
            finally
            {
                EditorRegistrationInfo.Default.Editors.Add(
                new EditorClassInfo(CustomEditName,
                                    typeof(PopupGridControlEdit), typeof(RepositoryItemPopupGridControlEdit),
                                    typeof(PopupGridControlEditViewInfo), new PopupGridControlEditPainter(), true, img));
            }
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemPopupGridControlEdit source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }

        public override void CreateDefaultButton()
        {
            Buttons.Add(new EditorButton(ButtonPredefines.Combo));
        }
    }

    public class PopupGridControlEditViewInfo : ButtonEditViewInfo
    {
        public PopupGridControlEditViewInfo(RepositoryItem item) : base(item)
        {
        }
    }

    public class PopupGridControlEditPainter : ButtonEditPainter
    {
        public PopupGridControlEditPainter()
        {
        }
    }
}
